//School of Informatics Xiamen University, GPL-3.0 license

package cn.edu.xmu.javaee.productdemomybatis.mapper.manual;

import cn.edu.xmu.javaee.productdemomybatis.mapper.generator.po.OnSalePo;
import cn.edu.xmu.javaee.productdemomybatis.mapper.generator.po.ProductPo;
import cn.edu.xmu.javaee.productdemomybatis.mapper.manual.po.ProductAllPo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface ProductMyBatisMapper extends ProductMapper{

    @Select({
            "select",
            "`id`, `shop_id`, `category_id`, `template_id`, `name`, `original_price`, ",
            "`weight`, `barcode`, `unit`, `origin_place`, `creator`, ",
            "`modifier`, `gmt_create`, `gmt_modified`, `status`, `commission_ratio`, ",
            "`shop_logistic_id`, `free_threshold`",
            "from goods_product",
            "where `name` = #{name,jdbcType=VARCHAR}"
    })
    @Results(id="FullProductMap", value = {
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="shop_id", property="shopId", jdbcType=JdbcType.INTEGER),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="original_price", property="originalPrice", jdbcType=JdbcType.INTEGER),
            @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
            @Result(column="barcode", property="barcode", jdbcType=JdbcType.VARCHAR),
            @Result(column="unit", property="unit", jdbcType=JdbcType.VARCHAR),
            @Result(column="origin_place", property="originPlace", jdbcType=JdbcType.VARCHAR),
            @Result(column="commission_ratio", property="commissionRatio", jdbcType=JdbcType.INTEGER),
            @Result(column="free_threshold", property="freeThreshold", jdbcType=JdbcType.INTEGER),
            @Result(column="status", property="status", jdbcType=JdbcType.SMALLINT),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="modifier", property="modifier", jdbcType=JdbcType.VARCHAR),
            @Result(column="gmt_create", property="gmtCreate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="gmt_modified", property="gmtModified", jdbcType=JdbcType.TIMESTAMP),
            @Result(property =  "onSaleList", javaType = List.class, many =@Many(select="selectLastOnSaleByProductId"), column = "id"),
            @Result(property =  "otherProduct", javaType = List.class, many =@Many(select="selectOtherProduct"), column = "id")
    })
    List<ProductAllPo> getProductByName(String name);

    @Select({
            "select",
            "`id`, `shop_id`, `category_id`, `template_id`, `name`, `original_price`, ",
            "`weight`, `barcode`, `unit`, `origin_place`, `creator`, ",
            "`modifier`, `gmt_create`, `gmt_modified`, `status`, `commission_ratio`, ",
            "`shop_logistic_id`, `free_threshold`",
            "from goods_product",
            "where `name` = #{name,jdbcType=VARCHAR} and `shop_id` = #{shopId}"
    })
    @ResultMap("FullProductMap")
    List<ProductAllPo> getProductByShopIdAndName(Integer shopId, String name);

    @Select({
            "select",
            "`id`, `shop_id`, `category_id`, `template_id`, `name`, `original_price`, ",
            "`weight`, `barcode`, `unit`, `origin_place`, `creator`, ",
            "`modifier`, `gmt_create`, `gmt_modified`, `status`, `commission_ratio`, ",
            "`shop_logistic_id`, `free_threshold`",
            "from goods_product",
            "where `id` = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="shop_id", property="shopId", jdbcType=JdbcType.INTEGER),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="original_price", property="originalPrice", jdbcType=JdbcType.INTEGER),
            @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
            @Result(column="barcode", property="barcode", jdbcType=JdbcType.VARCHAR),
            @Result(column="unit", property="unit", jdbcType=JdbcType.VARCHAR),
            @Result(column="origin_place", property="originPlace", jdbcType=JdbcType.VARCHAR),
            @Result(column="commission_ratio", property="commissionRatio", jdbcType=JdbcType.INTEGER),
            @Result(column="free_threshold", property="freeThreshold", jdbcType=JdbcType.INTEGER),
            @Result(column="status", property="status", jdbcType=JdbcType.SMALLINT),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="modifier", property="modifier", jdbcType=JdbcType.VARCHAR),
            @Result(column="gmt_create", property="gmtCreate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="gmt_modified", property="gmtModified", jdbcType=JdbcType.TIMESTAMP),
            @Result(property =  "onSaleList", javaType = List.class, many =@Many(select="selectLastOnSaleByProductId"), column = "id"),
            @Result(property =  "otherProduct", javaType = List.class, many =@Many(select="selectOtherProduct"), column = "id")
    })
    ProductAllPo getProductById(Integer id);

    @Select({
            "select",
            "`id`, `product_id`, `price`, `begin_time`, `end_time`, `quantity`, `max_quantity`,`creator`, ",
            "`modifier`, `gmt_create`, `gmt_modified`",
            "from goods_onsale",
            "where `product_id` = #{productId,jdbcType=BIGINT} and `begin_time` <= NOW() and `end_time` > NOW()"
    })
    @Results(id = "OnSaleMap", value = {
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="product_id", property="productId", jdbcType=JdbcType.INTEGER),
            @Result(column="price", property="price", jdbcType=JdbcType.INTEGER),
            @Result(column="begin_time", property="beginTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="end_time", property="endTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="quantity", property="quantity", jdbcType=JdbcType.INTEGER),
            @Result(column="max_quantity", property="maxQuantity", jdbcType=JdbcType.INTEGER),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="modifier", property="modifier", jdbcType=JdbcType.VARCHAR),
            @Result(column="gmt_create", property="gmtCreate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="gmt_modified", property="gmtModified", jdbcType=JdbcType.TIMESTAMP)
    })
    List<OnSalePo> selectLastOnSaleByProductId(Long productId);

    @Select({
            "select",
            "A.id as id,`shop_id`, `name`, `original_price`, `weight`, ",
            "`barcode`, `unit`, `origin_place`, A.creator as `creator`, A.modifier as `modifier`, ",
            "A.gmt_create as `gmt_create`, A.gmt_modified as `gmt_modified`",
            "from goods_product A",
            "inner join goods_goods B on A.id = B.relate_product_id",
            "where B.product_id= #{productId,jdbcType=INTEGER}"
    })
    @Results(id = "ProductMap", value = {
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="shop_id", property="shopId", jdbcType=JdbcType.INTEGER),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="original_price", property="originalPrice", jdbcType=JdbcType.INTEGER),
            @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER),
            @Result(column="barcode", property="barcode", jdbcType=JdbcType.VARCHAR),
            @Result(column="unit", property="unit", jdbcType=JdbcType.VARCHAR),
            @Result(column="origin_place", property="originPlace", jdbcType=JdbcType.VARCHAR),
            @Result(column="creator", property="creator", jdbcType=JdbcType.VARCHAR),
            @Result(column="modifier", property="modifier", jdbcType=JdbcType.VARCHAR),
            @Result(column="gmt_create", property="gmtCreate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="gmt_modified", property="gmtModified", jdbcType=JdbcType.TIMESTAMP)
    })
    ProductPo selectOtherProduct(Integer productId);
}
